<template>
  <div class="my_calendar">
    <section class="my_head">
      <!-- 年份 -->
      <el-select v-model="currentYear" size="small" placeholder="请选择年">
        <el-option v-for="item in yearList" :key="item" :label="item" :value="item" />
      </el-select>
      <!-- 月份 -->
      <el-select v-model="currentMonth" size="small" placeholder="请选择月">
        <!-- label是展示的数据 value是选中的数据,会同步给 el-select v-model的值 -->
        <el-option v-for="item in 12" :key="item" :label="item" :value="item" />
      </el-select>
    </section>
    <el-calendar v-model="value">
      <template #dateCell="{date,data}">
        <!-- {{ date }} 是日期对象 -->
        <!-- {{ data }} 是一个对象 这个对象中有三个属性 isSelected type day(日期字符串)  -->
        <!-- 周六周日休息 -->
        <!--
           <div class="date-content">
          <span class="text">{{ getDay(data.day) }}</span>
          <span v-if="isWeek(date)" class="rest">休</span>
        </div> -->
        <!-- 排班表休息 -->
        <div class="date-content">
          <span class="text">{{ getDay(data.day) }}</span>
          <span v-if="isWeek(data.day)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  props: {
    currentDate: {
      type: Date,
      // 复杂数据类型的默认值是一个工厂函数返回一的结果
      default: () => new Date()
    }
  },
  data() {
    return {
      value: new Date(),
      currentMonth: null, // 月份
      currentYear: null, // 年份
      // 年份列表是  前五年 + 今年 + 后五年
      // yearList: ['2016', '2017', '2018', '2019', '2020', '2021'] // 写死的年份列表
      // 这个weekList是模拟发送请求获取到的数组
      weekList: ['2021-12-04', '2021-12-14', '2021-12-24', '2021-12-18', '2021-12-19']
    }
  },
  computed: {
    // 前五年+今年+后五年
    yearList() {
      return Array.from(new Array(11), (item, index) => {
        return index + this.currentYear - 5
      })
    }
  },
  watch: {
    // 侦听年的变化 拼接一个新的日期对象并赋值给value
    currentYear(newval) {
      // console.log(newval)
      this.value = new Date(`${newval}-${this.currentMonth}`)
    },
    // 侦听月的变化 拼接一个新的日期对象并赋值给value
    currentMonth(newval) {
      // console.log(newval)
      this.value = new Date(`${this.currentYear}-${newval}`)
    },
    // 监听日历组件value的变换, 将最新的日期对象取出来年和月 赋值给 currentYear/currentMonth
    value(newval) {
      // console.log(newval.getFullYear())
      this.currentYear = newval.getFullYear()
      this.currentMonth = newval.getMonth() + 1
    }
  },
  created() {
    this.currentMonth = this.currentDate.getMonth() + 1
    this.currentYear = this.currentDate.getFullYear()
  },
  methods: {
    getDay(daystr) {
      // console.log(daystr)
      const day = daystr.split('-')[2] // 获取日
      // console.log(day)
      // console.log(day.slice('1'))
      const res = day.startsWith('0') ? day.slice('1') : day // 判断是否是以0开始的日, 是截取了
      return res
    },
    /* isWeek(date) {
      // console.log(date.getDay()) // 获取的周几 周一到周六 1-6 周日是0
      return date.getDay() === 0 || date.getDay() === 6
    } */
    isWeek(date) {
      // console.log(date)
      return this.weekList.includes(date)
    }
  }
}
</script>

<style lang="scss" scoped>
.my_calendar{
  ::v-deep.el-calendar__header{
    display: none;
  }
  .my_head{
    display: flex;
    justify-content: flex-end;
    .el-select{
      margin-left: 20px;
    }
  }
}

</style>

<style lang="scss" scoped>

::v-deep .el-calendar-day {
  height:  auto;
 }
::v-deep .el-calendar-table__row td::v-deep .el-calendar-table tr td:first-child, ::v-deep .el-calendar-table__row td.prev{
  border:none;
 }
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text{
  width: 20px;
  height: 20px;
  line-height: 20px;
 display: inline-block;

}
::v-deep .el-calendar-table td.is-selected .text{
   background: #409eff;
   color: #fff;
   border-radius: 50%;
 }
::v-deep .el-calendar__header {
   display: none
 }
</style>
